<template>
  <section class="todoapp">
    <TodoHeader @add="addfn"></TodoHeader>
    <TodoMain
      :list="list"
      @del="delfn"
      @change="changefn"
      :type="type"
      @checkAll="checkAllfn"
    ></TodoMain>
    <TodoFooter
      :list="list"
      @clearDone="clearfn"
      :type="type"
      @gaoliang="gaoliang"
    ></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from './components/TodoHeader.vue'
import TodoMain from './components/TodoMain.vue'
import TodoFooter from './components/TodoFooter.vue'
export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      // list: [
      //   { id: 1, name: '吃饭', isDone: true },
      //   { id: 2, name: '睡觉', isDone: true },
      //   { id: 3, name: '打豆豆', isDone: true },
      // ],
      type: 'all',
      list: JSON.parse(localStorage.getItem('todo')) || [],
    }
  },
  watch: {
    list: {
      handler(newlist) {
        localStorage.setItem('todo', JSON.stringify(newlist))
      },
      deep: true,
    },
  },
  methods: {
    delfn(id) {
      this.list = this.list.filter((item) => (item.id = !id))
    },
    changefn(id) {
      this.list.forEach((item) => {
        if (item.id === id) {
          item.isDone = !item.isDone
        }
      })
    },
    addfn(name) {
      // console.log(name)
      if (!name) {
        alert('请输入内容')
        return
      }
      this.list.unshift({
        id: Date.now(),
        name,
        isDone: false,
      })
    },
    clearfn() {
      this.list = this.list.filter((item) => item.isDone === false)
    },
    gaoliang(type) {
      this.type = type
    },
    checkAllfn(status) {
      this.list.forEach((item) => {
        item.isDone = status
      })
    },
  },
}
</script>

<style></style>
